<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 日息宝
 */
declare (strict_types=1);

namespace app\api\controller;

use app\api\base\controller\Base;
use app\api\validate\RixibaoValidate;
use app\extend\model\Level;
use app\extend\model\User;
use think\facade\Db;

class Rixibao extends Base
{
    /**
     * 列表
     */
    public function getList()
    {
        $validate = new RixibaoValidate();
        $ret = $validate->scene('index')->check(input());
        if (!$ret) {
            return json(_E($validate->getError()));
        }

        $page = input('page/d', 1);
        $limit = input('limit/d', 20);

        $where = [
            ['uid', '=', self::$UserInfo['id']]
        ];

        $list = [];
        $count = \app\extend\model\Rixibao::where($where)->count();
        if ($count > 0) {
            $list = \app\extend\model\Rixibao::where($where)->order(['id' => 'desc'])->page($page)->limit($limit)->select();
        }

        return json([
            'code' => 0,
            'msg' => '获取成功',
            'count' => $count,
            'data' => $list,
        ])->send();
    }

    /**
     * 详情
     */
    public function getDetail()
    {
        $rixibaoRate = Level::where([
            ['id', '=', self::$UserInfo['level_id']]
        ])->value('rixibao_rate');

        $income = \app\extend\model\Rixibao::where([
            ['uid','=',self::$UserInfo['id']],
            ['type','=',3]
        ])->sum('money');

        return json(_S('获取成功',[
            'rixibao_rate' => $rixibaoRate,
            'income' => $income
        ]));
    }

    /**
     * 存入
     */
    public function into()
    {
        if (request()->isPost()) {
            $validate = new RixibaoValidate();
            $ret = $validate->scene('into')->check(input());
            if (!$ret) {
                return json(_E($validate->getError()));
            }

            $money = input('money', 0);

            $userInfo = User::where([
                ['id', '=', self::$UserInfo['id']]
            ])->field('id,balance,rixibao_money')->find();

            if ($userInfo->balance < $money) {
                return json(_E('账户余额不足'));
            }

            try {
                // 启动事务
                Db::startTrans();

                \app\extend\model\Rixibao::insert([
                    'uid' => self::$UserInfo['id'],
                    'money' => $money,
                    'type' => 1,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s')
                ]);

                User::where([
                    'id' => self::$UserInfo['id']
                ])->update([
                    'balance' => $userInfo->balance - $money,
                    'rixibao_money' => $userInfo->rixibao_money + $money,
                    'updated_at' => date('Y-m-d H:i:s')
                ]);

                Db::commit();
                return json(_S('存入成功'));
            } catch (\Exception $e) {
                Db::rollback();
                return json(_E('存入失败'));
            }
        }
    }

    /**
     * 取出
     */
    public function out()
    {
        if (request()->isPost()) {
            $validate = new RixibaoValidate();
            $ret = $validate->scene('out')->check(input());
            if (!$ret) {
                return json(_E($validate->getError()));
            }

            $money = input('money', 0);

            $userInfo = User::where([
                ['id', '=', self::$UserInfo['id']]
            ])->field('id,balance,rixibao_money')->find();

            if ($userInfo->rixibao_money < $money) {
                return json(_E('日息宝余额不足'));
            }

            try {
                // 启动事务
                Db::startTrans();

                \app\extend\model\Rixibao::insert([
                    'uid' => self::$UserInfo['id'],
                    'money' => $money,
                    'type' => 2,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s')
                ]);

                User::where([
                    'id' => self::$UserInfo['id']
                ])->update([
                    'balance' => $userInfo->balance + $money,
                    'rixibao_money' => $userInfo->rixibao_money - $money,
                    'updated_at' => date('Y-m-d H:i:s')
                ]);

                Db::commit();
                return json(_S('取出成功'));
            } catch (\Exception $e) {
                Db::rollback();
                return json(_E('取出失败'));
            }
        }
    }
}
